﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using EvacuationWidth.ModelLayer;
using EvacuationWidth.ViewLayer;
using YArchitech.Controls;
using YJKCodeBase.WinForm;
using YJKCodeBase.WinForm.BackGroundProgressBar;
using ModelCheckUtility;
using ModelCheckUtility.ControlLayer;

namespace EvacuationWidth.ControlLayer
{
	public class EvacuationWidthManger
	{
		public EvacuationWidthManger(ExternalCommandData input)
		{
			this.m_useCommand = input;
			this.m_uiApp = input.Application;
			this.m_app = this.m_uiApp.Application;
			this.m_uiDoc = this.m_uiApp.ActiveUIDocument;
			this.m_doc = this.m_uiDoc.Document;
			Process currentProcess = Process.GetCurrentProcess();
			IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
			this.m_RevitHandle = new WindowHandle(mainWindowHandle);
			RevitVersionFuncs.ClearSelection(this.m_uiDoc.Selection);
		}

		public void DoWork()
		{
			this.m_useLevelGeter = new LevelGeter(this.m_doc);
			this.m_lstSelectLevel = new List<Level>();
			bool selectLevel = this.m_useLevelGeter.GetSelectLevel(ref this.m_lstSelectLevel);
			if (!selectLevel || this.m_lstSelectLevel.Count == 0)
			{
				return;
			}
			string xmlPath = string.Empty;
            string appRootPath = ModelCheckUtility.Utility.GetAppRootPath();
			xmlPath = appRootPath + "\\Data\\";
			this.m_selectForm = new BuildingTypeSelectForm(xmlPath, this.m_useLevelGeter.LstAllLevels);
			if (this.m_selectForm.UseDocument == null)
			{
				YJKMessageBox.Show("没有获取到配置文件请检查");
				return;
			}
			this.m_selectForm.ShowDialog();
			this.m_useRuleBean = this.m_selectForm.ReturnValue;
			if (this.m_useRuleBean == null)
			{
				return;
			}
            ModelCheckUtility.Utility.AddRoomToNoneRoomPlace(this.m_doc, this.m_lstSelectLevel);
			ProgressBarManger progressBarManger = new ProgressBarManger(new BackWorkDelegate(this.BackGroundCalculate));
			progressBarManger.ShowFormAndDoWork(this.m_RevitHandle);
			if (progressBarManger.IfManualAbsort)
			{
				return;
			}
			this.m_useResultShowManger = new ResultShowManger(this.m_lstAllResultBean, this.m_uiDoc, this.m_RevitHandle, this.m_nAreaCount);
			this.m_useResultShowManger.ShowWithWhile();
            ModelCheckUtility.Utility.DeleteAddRoom(this.m_doc, new List<int>());
			ElementPackerManger.ReSet();
		}

		private void BackGroundCalculate()
		{
			this.m_useAreaGeter = new AreaGeter();
			this.m_lstAllGetedArea = this.m_useAreaGeter.GetAllAreas(this.m_doc, this.m_lstSelectLevel);
			this.m_nAreaCount = this.m_lstAllGetedArea.Count;
			for (int i = 0; i < this.m_lstAllGetedArea.Count; i++)
			{
				AreaPacker_EvacuationWidth areaPacker_EvacuationWidth = this.m_lstAllGetedArea[i];
				areaPacker_EvacuationWidth.SetBuildingRule(this.m_useRuleBean);
				AreaCheckResultBean item;
				if (areaPacker_EvacuationWidth.TryCheck(out item))
				{
					this.m_lstAllResultBean.Add(item);
				}
			}
		}

		private ExternalCommandData m_useCommand;

        private Autodesk.Revit.ApplicationServices.Application m_app;

		private UIApplication m_uiApp;

		private Document m_doc;

		private UIDocument m_uiDoc;

		private IWin32Window m_RevitHandle;

		private LevelGeter m_useLevelGeter;

		private AreaGeter m_useAreaGeter;

		private BuildingTypeSelectForm m_selectForm;

		private ResultShowManger m_useResultShowManger;

		private List<Level> m_lstSelectLevel = new List<Level>();

		private List<AreaPacker_EvacuationWidth> m_lstAllGetedArea = new List<AreaPacker_EvacuationWidth>();

		private List<AreaCheckResultBean> m_lstAllResultBean = new List<AreaCheckResultBean>();

		private BuildingRuleBean m_useRuleBean;

		private int m_nAreaCount;
	}
}
